﻿using System;
using UnityEngine;

[Serializable]
public class CameraDragByCurve : CameraDrag
{
    [SerializeField] private float speed = 1;
    [Space(20)]
    [SerializeField] private AnimationCurve[] m_Curves;
    [Tooltip("曲线列表默认索引")]
    [SerializeField] int AnimationCurveIndex =0;
    [Space(20)]
    private float value;

    private AnimationCurve Curve
    {
        get
        {
            if (AnimationCurveIndex>-1&&AnimationCurveIndex<m_Curves.Length)
            {
                return m_Curves[AnimationCurveIndex];
            }

            return null;
        }
    }

    protected override void LaunchCameraMove()
    {
        base.LaunchCameraMove();
        value = 0;
    }

    protected override void OnCameraMove()
    {
        base.OnCameraMove();
        if (Curve==null)
        {
            return;;
        }
        if (!isLaunchMove)
        {
            return;
        }

        value += Time.deltaTime * speed;
        if (value > 1)
        {
            value = 1;
            isLaunchMove = false;
        }

        float v = Curve.Evaluate(value);
        Vector3 result = Vector3.Lerp(curDragComeraPos, cameraTargetPos, v);
        SetCameraPos(result);
    }
}